import 'package:flutter/material.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/widgets/common/xindong_common_widget.dart';
import 'package:xindong/widgets/xindong_loading.dart';
import 'package:video_player/video_player.dart';

// 视频层 + 加载圈圈
class XinDongCommonVideoPlayerBigLayer extends StatefulWidget {
  final VideoPlayerController? videoPlayerController;
  final Widget? cover; // 如果有封面图则在加载完整前显示该视图
  final BoxFit fit; // cover：裁剪铺满屏，fitWidth：适配宽度，宽度铺满，高留边或者裁剪

  const XinDongCommonVideoPlayerBigLayer(this.videoPlayerController, {Key? key, this.cover, this.fit = BoxFit.fitWidth}) : super(key: key);

  @override
  State<XinDongCommonVideoPlayerBigLayer> createState() => _XinDongCommonVideoPlayerBigLayerState();
}

class _XinDongCommonVideoPlayerBigLayerState extends State<XinDongCommonVideoPlayerBigLayer> {
  bool isBuffering = true;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      widget.videoPlayerController?.addListener(() async {
        // 播放错误
        if (widget.videoPlayerController!.value.hasError == true) {
          return;
        }

        // 资源时长为0
        if (widget.videoPlayerController!.value.duration.inMilliseconds == 0.0) {
          return;
        }

        try {
          if (isBuffering != widget.videoPlayerController?.value.isBuffering) {
            isBuffering = widget.videoPlayerController?.value.isBuffering ?? true;
            setState(() {});
          }
        } catch (error) {
          mCatchPrint(error);
        }
      });
    });
  }

  @override
  void dispose() {
    widget.videoPlayerController?.removeListener(() {});
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.videoPlayerController?.value.isInitialized == true) {
      bool isLandscape = widget.videoPlayerController!.value.aspectRatio > 1.0;
      return Stack(
        children: [
          isLandscape
              ? Center(
                  child: AspectRatio(
                    aspectRatio: widget.videoPlayerController!.value.aspectRatio,
                    child: VideoPlayer(widget.videoPlayerController!),
                  ),
                )
              : Container(
                  clipBehavior: Clip.antiAlias,
                  decoration: const BoxDecoration(),
                  child: SizedBox.expand(
                    child: FittedBox(
                      fit: widget.fit,
                      child: SizedBox(
                        height: 1,
                        width: widget.videoPlayerController!.value.aspectRatio,
                        child: VideoPlayer(widget.videoPlayerController!),
                      ),
                    ),
                  ),
                ),
          // 缓存加载
          widget.videoPlayerController?.value.isBuffering == true ? const XinDongLoadingCenter() : const SizedBox(),
        ],
      );
    } else {
      // 未初始化加载
      return widget.cover != null ? widget.cover! : const XinDongLoadingCenter();
    }
  }
}

// 小屏视频层 + 封面 + 播放图标
// 不需要监听，不区分横竖屏
class XinDongCommonVideoPlayerSmallLayer extends StatelessWidget {
  final VideoPlayerController videoPlayerController;
  final String videoKey; // 想控制
  final String? cover; // 如果有封面图则在加载完整前显示该视图

  const XinDongCommonVideoPlayerSmallLayer({Key? key, required this.videoPlayerController, required this.videoKey, this.cover}) : super(key: key);


  @override
  Widget build(BuildContext context) {
    bool isPlaying = videoPlayerController.value.isInitialized && videoPlayerController.value.isPlaying;
    return Stack(
      alignment: Alignment.center,
      children: [
        videoPlayerController.value.isInitialized
            ? Hero(
                tag: videoKey,
                child: Center(
                  child: SizedBox.expand(
                    child: FittedBox(
                      fit: BoxFit.cover,
                      child: SizedBox(
                        height: 1,
                        width: videoPlayerController.value.aspectRatio,
                        child: VideoPlayer(videoPlayerController),
                      ),
                    ),
                  ),
                ),
              )
            : videoPlayerCoverLayer(cover),
        isPlaying ? const SizedBox() : videoPlayerPauseLayer,
      ],
    );
  }
}
